﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DITCharts.DITCharts.PieChart;
using System.Collections.ObjectModel;
using TestStarter.Model;

namespace DITCharts.Controls.PieChart
{
    /// <summary>
    /// Defines the layout of the pie chart
    /// </summary>
    public partial class PieChartLayout : UserControl
    {
        #region dependency properties

        /// <summary>
        /// The property of the bound object that will be plotted (CLR wrapper)
        /// </summary>
        public String ValueMember
        {
            get { return GetValueMember(this); }
            set { SetValueMember(this, value); }
        }

        // PlottedProperty dependency property
        public static readonly DependencyProperty ValueMemberProperty =
                       DependencyProperty.RegisterAttached("ValueMember", typeof(String), typeof(PieChartLayout),
                       new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.Inherits));

        // PlottedProperty attached property accessors
        public static void SetValueMember(UIElement element, String value)
        {
            element.SetValue(ValueMemberProperty, value);
        }
        public static String GetValueMember(UIElement element)
        {
            return (String)element.GetValue(ValueMemberProperty);
        }


        /// <summary>
        /// The property of the bound object that will be plotted (CLR wrapper)
        /// </summary>
        public String TitleMember
        {
            get { return GetTitleMember(this); }
            set { SetTitleMember(this, value); }
        }

        // PlottedProperty dependency property
        public static readonly DependencyProperty TitleMemberProperty =
                       DependencyProperty.RegisterAttached("TitleMember", typeof(String), typeof(PieChartLayout),
                       new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.Inherits));

        // PlottedProperty attached property accessors
        public static void SetTitleMember(UIElement element, String value)
        {
            element.SetValue(TitleMemberProperty, value);
        }
        public static String GetTitleMember(UIElement element)
        {
            return (String)element.GetValue(TitleMemberProperty);
        }


        /// <summary>
        /// A class which selects a color based on the item being rendered.
        /// </summary>
        public IColorSelector ColorSelector
        {
            get { return GetColorSelector(this); }
            set { SetColorSelector(this, value); }
        }

        // ColorSelector dependency property
        public static readonly DependencyProperty ColorSelectorProperty =
                       DependencyProperty.RegisterAttached("ColorSelectorProperty", typeof(IColorSelector), typeof(PieChartLayout),
                       new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));

        // ColorSelector attached property accessors
        public static void SetColorSelector(UIElement element, IColorSelector value)
        {
            element.SetValue(ColorSelectorProperty, value);
        }
        public static IColorSelector GetColorSelector(UIElement element)
        {
            return (IColorSelector)element.GetValue(ColorSelectorProperty);
        }






        /// <summary>
        /// The property for the Data Source
        /// </summary>
        public ObservableCollection<ChartData> DataSourceItems
        {
            get { return GetDataSourceItems(this); }
            set { SetDataSourceItems(this, value); }
        }

        // DataSource dependency property
        public static readonly DependencyProperty DataSourceItemsProperty =
                       DependencyProperty.RegisterAttached("DataSourceItems", typeof(ObservableCollection<ChartData>), typeof(PieChartLayout),
                       new FrameworkPropertyMetadata(new ObservableCollection<ChartData>(), FrameworkPropertyMetadataOptions.Inherits));

        // DataSource attached property accessors
        public static void SetDataSourceItems(UIElement element, ObservableCollection<ChartData> value)
        {
            element.SetValue(DataSourceItemsProperty, value);
        }
        public static ObservableCollection<ChartData> GetDataSourceItems(UIElement element)
        {
            return (ObservableCollection<ChartData>)element.GetValue(DataSourceItemsProperty);
        }

        private static void OnDataSourceItemsPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            //DataContex = sender;

            var element = new PiePlotter() as UIElement;

            var asss = sender as PieChartLayout;

            //SetDataSourceItems(element,
            //ObservableCollection<ChartData> Dataems = asss.DataSourceItems;

            //SetDataSourceItems(element, Dataems);

            string text = e.ToString();


        }


        public static double PieWidth { get; set; }
        public static double PieHeight { get; set; }

        #endregion

        public PieChartLayout()
        {
            InitializeComponent();

            PieWidth = this.Width != 0 ? this.Width : 100;
            PieHeight = this.Height != 0 ? this.Height : 100; 
        }
    }
}
